package com.wis.config;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

@Configuration
@Slf4j
public class MyWebAppConfigure implements WebMvcConfigurer {
    @Value("${upload.upload-dir-windows}")
    private String windowsFilepathPre;

    @Value("${upload.upload-dir-linux}")
    private String linuxFilepathPre;

    @Value("${upload.upload-choose}")
    private int choose;

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        if (choose == 1){
            String windowsFilepath = windowsFilepathPre.replace('\\', '/');
            //win
            log.info("windows路径 = "+ "file:"+windowsFilepath+"/");
            registry.addResourceHandler("/**")
                    .addResourceLocations("file:"+windowsFilepath+"/")
                    .addResourceLocations("classpath:/static/");
        } else { //linux
            log.info("linux路径 ="+ "file:"+linuxFilepathPre+"/");
            registry.addResourceHandler("/**")
                    .addResourceLocations("file:"+linuxFilepathPre+"/")
                    .addResourceLocations("classpath:/static/");
        }

    }
    @Override
    public void addCorsMappings(CorsRegistry registry) {
        registry.addMapping("/**")
                .allowedOrigins(
                        "http://localhost:8080"
                ) // 明确列出允许的来源
                .allowedMethods("GET", "POST", "PUT", "DELETE")
                .allowedHeaders("*")
                .allowCredentials(true);
    }
}
